package cConfig

import (
	"gitee.com/csingo/cComponents"
	"gitee.com/csingo/cLog"
)

type ConfigComponent struct{}

func (i *ConfigComponent) Inject(instance any) bool {
	if container.Is(instance) {
		return container.Save(instance)
	}

	if container.IsConfigListener(instance) {
		return container.SaveConfigListener(instance.(*cComponents.ConfigListener))
	}

	return false
}

func (i *ConfigComponent) InjectConf(config cComponents.ConfigInterface) bool {
	if config.ConfigName() == ConfigCenterConfigName {
		config_center_config = config.(*ConfigCenterConf)
		if config_center_config.Layers < 0 {
			config_center_config.Layers = 0
		}
		return true
	}

	return false
}

func (i *ConfigComponent) Load() {
	if !config_center_config.Enable {
		cLog.WithContext(nil, map[string]any{}, []cLog.Option{cLog.IncludePaths{}}).Warn("配置中心未开启")
		return
	}

	// 启动配置监听
	load()
}

func (i *ConfigComponent) Listen() []*cComponents.ConfigListener {
	return []*cComponents.ConfigListener{
		{
			ConfigName: ConfigCenterConfigName,
			Listeners: []cComponents.ListenHandler{
				cComponents.DefaultListenHandler,
			},
		},
	}
}

var Component = &ConfigComponent{}
